/**
 * ElectronDaoImpl.java
 */
package com.prs.crm.dao.document.electron.impl;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

import org.courser.hibernate.QueryExample;
import org.courser.ui.PaginationSupport;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.document.electron.ElectronDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.document.electron.Electron;
import com.prs.crm.domain.document.rule.Rule;

/**
 * @author ZhuDongMei
 * @E-mail:1223984339@qq.com
 * @qq:1223984339
 * @version 创建时间：2011-3-16 上午11:12:28 简单说明
 */

public class ElectronDaoImpl extends BaseDaoImpl<Electron> implements ElectronDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<Electron> getAllElectrons() {
		return (List<Electron>)this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
			throws HibernateException, SQLException {
		// TODO Auto-generated method stub
		Criteria criteria = session.createCriteria(Electron.class);
		criteria.addOrder(Order.asc("id"));
		return criteria.list();
	}

		});
	}

	@Override
	public Electron getElectronById(Serializable id) {
		return this.getHibernateTemplate().get(Electron.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Electron> getRootElectron() {
		Criteria criteria=this.getSession().createCriteria(Electron.class).add(Restrictions.isNull("parent"))
        .add(Restrictions.eq("type", "类别"));
         return criteria.list();
	}
	@Override
	public int removeElectron(Integer id) {
		Query query=this.getSession().createQuery("delete from Electron where id="+id);
		return query.executeUpdate();
	}

}
